#include "stdafx.h"
#include "ChatModule.h"
#include "ChatFakeNet.h"

ChatLogicModule::ChatLogicModule()
{
}

ChatLogicModule::~ChatLogicModule()
{
}

void ChatLogicModule::Run()
{
	Init();
}

void ChatLogicModule::Init()
{
	m_MyInfo.Account = _T("MySelf");

	FakeNetModule::Instance().clientRecvMessage.connect(this, &ChatLogicModule::OnRecvMessage);

	SendLoginMessage();
}

std::TString ChatLogicModule::GetRecverAccount(SessionId id)
{
	auto it = m_ChatSessions.find(id);
	if (it==m_ChatSessions.end())
	{
		return _T("");
	}

	return it->second.Accounts;
}

void ChatLogicModule::CreateSession(const std::TString& account)
{
	SessionId sessionId = Utils::GenerateSessionId(account);
	auto it = m_ChatSessions.find(sessionId);
	if (it!= m_ChatSessions.end())
	{
		return;
	}

	ChatSession session;
	session.ID = sessionId;
	session.Accounts = account;
	
	m_ChatSessions[sessionId] = session;

	newSession(sessionId);
}

void ChatLogicModule::SendLoginMessage()
{
	StringList msglist;
	FakeNetModule::Instance().SendMessage(C_S_GET_FRIENDLIST, msglist);
}

void ChatLogicModule::SendChatMessage(SessionId sessionId, const std::TString& msg)
{
	std::TString sender = m_MyInfo.Account;
	std::TString recver = GetRecverAccount(sessionId);

	StringList msglist = { sender, recver, msg };
	FakeNetModule::Instance().SendMessage(C_S_CHAT_MESSAGE, msglist);
}

void ChatLogicModule::SendSettingMessage(int settingType, const std::TString& val)
{
	StringList msg; msg.push_back(val);
	FakeNetModule::Instance().SendMessage(C_S_SET_FRIEND_INFO, msg);
}

void ChatLogicModule::OnRecvMessage(int msgId, const StringList& msg)
{
	if (msgId == S_C_CHAT_MESSAGE)
	{
		OnRecvChatMessage(msg);
	}
	else if (msgId == S_C_GET_FRIENDLIST)
	{
		OnRecvFriendListMessage(msg);
	}
}

void ChatLogicModule::OnRecvFriendListMessage(const StringList& msg)
{
	for (const auto& account : msg)
	{
		FriendId friendId = Utils::GenerateFriendId(account);
		Friend f = {friendId, account, account, _T("") ,_T("") ,_T(""), 0};
		m_Friends[friendId] = f;
	}

	recvFriendList();
}

void ChatLogicModule::OnRecvChatMessage(const StringList& msg)
{
	if (msg.size() != 3)
	{
		return;
	}

	const std::TString& sender = msg[0];
	const std::TString& recver = msg[1];
	const std::TString& content = msg[2];

	std::TString account;
	if (sender==m_MyInfo.Account)
	{
		account = recver;
	}
	else
	{
		account = sender;
	}

	ChatMsg chatMsg;
	chatMsg.ID = Utils::GenerateChatId();
	chatMsg.SenderAccount = sender;
	chatMsg.ReceiverAccount = recver;
	chatMsg.MsgContent = content;

	SessionId sessionId = Utils::GenerateSessionId(account);

	auto it = m_ChatSessions.find(sessionId);
	if (it==m_ChatSessions.end())
	{
		ChatSession session;
		session.ID = sessionId;
		session.Accounts = account;
		session.ChatList[chatMsg.ID] = chatMsg;

		// new Session
		m_ChatSessions[sessionId] = session;
	}
	else
	{
		// add ChatMsg
		ChatSession& session = it->second;
		session.ChatList[chatMsg.ID] = chatMsg;
	}

	recvChatMsg(sessionId, chatMsg.ID);
}



const FriendMap& ChatLogicModule::GetFriends()
{
	return m_Friends;
}

const ChatSession* ChatLogicModule::GetChatSession(SessionId sessionId)
{
	ChatSessionMap::iterator it = m_ChatSessions.find(sessionId);
	if (it != m_ChatSessions.end())
	{
		return &it->second;
	}

	return NULL;
}

const ChatMsg* ChatLogicModule::GetChatMessage(SessionId sessionId, ChatMsgId msgId)
{
	ChatSessionMap::const_iterator it = m_ChatSessions.find(sessionId);
	if (it == m_ChatSessions.end())
	{
		return NULL;
	}

	const ChatSession& session = it->second;
	const ChatMsgMap& msgMap = session.ChatList;
	ChatMsgMap::const_iterator itMsg = msgMap.find(msgId);
	if (itMsg == msgMap.end())
	{
		return NULL;
	}

	return &itMsg->second;
}

std::TString ChatLogicModule::GetIconFromAccount(const std::TString& account)
{
	return _T("file='image/friend_icon.png' source='0,0,20,20' dest='0,0,20,20'");
}
std::TString ChatLogicModule::GetNameFromAccount(const std::TString& account)
{
	return account;
}
std::TString ChatLogicModule::GetAccountFromName(const std::TString& name)
{
	return name;
}

std::TString ChatLogicModule::GetMyAccount()
{
	return m_MyInfo.Account;
}

std::TString ChatLogicModule::GetSessionAccount(SessionId sessionId)
{
	ChatSessionMap::iterator it = m_ChatSessions.find(sessionId);
	if (it != m_ChatSessions.end())
	{
		return it->second.Accounts;
	}

	return _T("---");
}

std::TString ChatLogicModule::GetSessionName(SessionId sessionId)
{
	return GetSessionAccount(sessionId);
}


void ChatLogicModule::ChangeFriendSetting(const std::TString& account, 
	int settingType, const CVar& val)
{
	FriendId friendId = Utils::GenerateFriendId(account);
	auto it = m_Friends.find(friendId);
	if (it==m_Friends.end())
	{
		return;
	}
	Friend& friendInfo = it->second;
	if (settingType == FriendSetting_CommentName)
	{
		friendInfo.CommentName = val.WideStrVal();
	}
	else if (settingType == FriendSetting_GroupName)
	{
		friendInfo.GroupName = val.WideStrVal();
	}
	else if (settingType == FriendSetting_Authority)
	{
		friendInfo.Authority = val.IntVal();
	}

	SendSettingMessage(settingType, val.ToWideStr());
}